//
//  Remark.m
//  Guazi
//
//  Created by koolearn on 11-7-31.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "Remark.h"
#import "DBConnection.h"
#import "StringUtil.h"
#import "GuaziAppDelegate.h"

#define kRemarkAllColumns @"r.id, r.user_id, r.status_id, r.text, r.created_at, r.source, r.type"


@implementation Remark

@synthesize remarkId;
@synthesize userId;
@synthesize statusId;
@synthesize text;
@synthesize type;
@synthesize createAt;
@synthesize source;
@synthesize user;



- (id) initWithJsonDictionary:(NSDictionary*)dic statusId:(long long)sId
{
    self = [super init];
    if ( self ) {
        self.remarkId = [[dic objectForKey:@"id"] longLongValue];
        self.statusId = sId;
        self.text = [dic objectForKey:@"text"];
        self.type = [dic objectForKey:@"remarkType"];
        self.source = [dic objectForKey:@"source"];
        
        NSString* time = [dic objectForKey:@"created_at"];
        NSDate* date = [time parseDateWithFormat:@"yyyy-MM-dd HH:mm:ss"];
        self.createAt = [date timeIntervalSince1970];
        
        id obj = [dic objectForKey:@"user"];
        if ( obj && [obj isKindOfClass:[NSDictionary class]]) {
            NSDictionary* dic = (NSDictionary*) obj;
            User* u = [[User alloc] initWithJsonDictionary:dic];
            self.user = u;
            self.userId = u.userId;
            [user release];
        }
    }
    return self;
}


- (id) initWithStatement:(Statement*)stmt
{
    self = [super init];
    if ( self ) {
        self.remarkId = [stmt getInt64:0];
        self.userId = [stmt getInt64:1];
        self.statusId = [stmt getInt64:2];
        self.text = [stmt getString:3];
        self.createAt = [stmt getInt64:4];
        self.source = [stmt getString:5];
        self.type = [stmt getString:6];
    }
    return self;
}


+ (NSMutableArray*) loadFromDB:(long long)statusId rows:(NSInteger)rows beforeTime:(time_t)time
{
    NSString* sql = nil;
    if ( time > 0 ) {
        sql = [NSString stringWithFormat:@"select %@, u.screen_name from remarks r, users u where r.user_id = u.user_id and r.status_id = ? and r.created_at < ? order by created_at desc", kRemarkAllColumns];
    }
    else {
        sql = [NSString stringWithFormat:@"select %@, u.screen_name from remarks r, users u where r.user_id = u.user_id and r.status_id = ? order by created_at desc", kRemarkAllColumns];
    }
    
    if ( rows > 0 ) {
        sql = [NSString stringWithFormat:@"%@ limit ?", sql];
    }
    
    sqlite3* db = [DBConnection getDatabase];
    Statement* stmt = [[Statement alloc] initWithDB:db sql:(char*)[sql UTF8String]];
    [stmt bindInt64:statusId forIndex:1];
    
    if ( time > 0 ) {
        [stmt bindInt64:time forIndex:2];
    }
    
    if ( rows > 0 ) {
        [stmt bindInt32:rows forIndex:3];
    }
    
    
    NSMutableArray* array = [NSMutableArray array];
    Remark* remark = nil;
    User* user = nil;
    while ( [stmt step] == SQLITE_ROW ) {
        remark = [[Remark alloc] initWithStatement:stmt];
        user = [[User alloc] init];
        user.userId = [stmt getInt64:1];
        user.screenName = [stmt getString:7];
        remark.user = user;
        [array addObject:remark];
    }

    [stmt release];
    return array;
}


- (void) insertDB
{
    char* sql = "replace into remarks (id, user_id, status_id, text, created_at, source, type) values (?,?,?,?,?,?,?)";
    sqlite3* db = [DBConnection getDatabase];
    Statement* stmt = [[Statement alloc] initWithDB:db sql:sql];
    [stmt bindInt64:remarkId forIndex:1];
    [stmt bindInt64:userId forIndex:2];
    [stmt bindInt64:statusId forIndex:3];
    [stmt bindString:text forIndex:4];
    [stmt bindInt64:createAt forIndex:5];
    [stmt bindString:source forIndex:6];
    [stmt bindString:type forIndex:7];
    
    if ([stmt step]!=SQLITE_DONE) {
        [stmt release];
		NSAssert2( 0, @"Fail to execute sql: %s (%s)", sql, sqlite3_errmsg(db) );
		return;
    }
    else {
        [stmt release];
    }
}


- (void) dealloc
{
    [text release];
    [type release];
    [source release];
    [user release];
    [super dealloc];
}


@end
